package com.cdm.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.cdm.pojo.Evaluate;
import com.cdm.service.EvaluateService;

@Controller
@RequestMapping("evaluate")
public class EvaluateController {

	@Autowired
	private EvaluateService evaluateService;
	
	@Autowired
	private HttpServletRequest request;
	
	
	
	@RequestMapping("/getEvaluate")
	public void getEvaluate(HttpServletResponse response) throws IOException{
		Map<String, Object> map=new HashMap<String, Object>();
		Integer pageNow  = Integer.parseInt(request.getParameter("pageNow"));
		Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
		int activityId = Integer.parseInt(request.getParameter("activityId"));
		map.put("index", (pageNow-1)*pageSize);
		map.put("pageSize", pageSize);
		map.put("activityId", activityId);
		List<Evaluate> evaluates = evaluateService.getEvaluates(map);
		Integer infoSum = evaluateService.getEvaluateCount(map);
		JSONObject jsonObject = new JSONObject();
		if (evaluates!=null && evaluates.size()>0) {
			jsonObject.put("result", "sucess");
		}else{
			jsonObject.put("result", "false");
		}
		jsonObject.put("pageNow", pageNow);
		jsonObject.put("pageSize", pageSize);
		jsonObject.put("infoSum", infoSum);
		jsonObject.put("pageSum", infoSum%pageSize==0?infoSum/pageSize:infoSum/pageSize+1);
		jsonObject.put("evaluateList", evaluates);
		PrintWriter writer = response.getWriter();
		writer.print(jsonObject);
		writer.flush();
		writer.close();
	}
	
	@RequestMapping("/getMyEvaluate")
	public void getMyEvaluate(HttpServletResponse response) throws IOException{
		Map<String, Object> map=new HashMap<String, Object>();
		Integer pageNow  = Integer.parseInt(request.getParameter("pageNow"));
		Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
		int userId = Integer.parseInt(request.getParameter("userId"));
		map.put("index", (pageNow-1)*pageSize);
		map.put("pageSize", pageSize);
		map.put("userId", userId);
		List<Map<String, Object>> evaluates = evaluateService.findMyEvaluate(map);
		Integer infoSum = evaluateService.findMyEvaluateCount(map);
		JSONObject jsonObject = new JSONObject();
		if (evaluates!=null && evaluates.size()>0) {
			jsonObject.put("result", "sucess");
		}else{
			jsonObject.put("result", "false");
		}
		jsonObject.put("pageNow", pageNow);
		jsonObject.put("pageSize", pageSize);
		jsonObject.put("infoSum", infoSum);
		jsonObject.put("pageSum", infoSum%pageSize==0?infoSum/pageSize:infoSum/pageSize+1);
		jsonObject.put("evaluateList", evaluates);
		PrintWriter writer = response.getWriter();
		writer.print(jsonObject);
		writer.flush();
		writer.close();
	}
	
	@RequestMapping("/addEvaluate")
	public void addEvaluate(HttpServletResponse response) throws IOException{
		Map<String, Object> map=new HashMap<String, Object>();
		Integer userId  = Integer.parseInt(request.getParameter("userId"));
		String evaluateContent = request.getParameter("evaluateContent");
		int activityId = Integer.parseInt(request.getParameter("activityId"));
		map.put("userId", userId);
		map.put("evaluateContent", evaluateContent);
		map.put("activityId", activityId);
		JSONObject jsonObject = new JSONObject();
		Boolean sucess = evaluateService.addEvaluate(map);
		jsonObject.put("result", sucess);
		PrintWriter writer = response.getWriter();
		writer.print(jsonObject);
		writer.flush();
		writer.close();
	}
	
	@RequestMapping("/deleteEvaluate")
	public void deleteEvaluate(HttpServletResponse response) throws Exception{
		Map<String, Object> map=new HashMap<String, Object>();
		Integer activityId  = Integer.parseInt(request.getParameter("activityId"));
		Integer userId  = Integer.parseInt(request.getParameter("userId"));
		String evaTime = request.getParameter("evaTime");
		System.out.println("=="+activityId+userId);
		map.put("activityId", activityId);
		map.put("userId", userId);
		map.put("evaTime", evaTime);
		Boolean sucess = evaluateService.deleteEvaluate(map);
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("result", sucess);
		PrintWriter write = response.getWriter();
		write.println(jsonObject);
		write.flush();
		write.close();
	}
	
}
